home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / checkbox / contrib / bpickle.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  7.3 KB  |  190 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''
  5. Copyright (c) 2006, Gustavo Niemeyer <gustavo@niemeyer.net>
  6.  
  7. All rights reserved.
  8.  
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11.  
  12.     * Redistributions of source code must retain the above copyright notice,
  13.       this list of conditions and the following disclaimer.
  14.     * Redistributions in binary form must reproduce the above copyright notice,
  15.       this list of conditions and the following disclaimer in the documentation
  16.       and/or other materials provided with the distribution.
  17.     * Neither the name of the copyright holder nor the names of its
  18.       contributors may be used to endorse or promote products derived from
  19.       this software without specific prior written permission.
  20.  
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  25. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. '''
  33. dumps_table = { }
  34. loads_table = { }
  35.  
  36. def dumps(obj, _dt = None):
  37.     if not _dt:
  38.         _dt = dumps_table
  39.     
  40.     type_names = [
  41.         type(obj)]
  42.     for type_name in type_names:
  43.         if _dt.has_key(type_name):
  44.             return _dt[type_name](obj)
  45.         type_names.extend(type_name.__bases__)
  46.     
  47.     raise ValueError, 'Unsupported type: %s' % type(obj)
  48.  
  49.  
  50. def loads(str, _lt = loads_table):
  51.     if not str:
  52.         raise ValueError, "Can't load empty string"
  53.     str
  54.     
  55.     try:
  56.         return _lt[str[0]](str, 0)[0]
  57.     except KeyError:
  58.         e = None
  59.         raise ValueError, 'Unknown type character: %s' % e
  60.     except IndexError:
  61.         raise ValueError, 'Corrupted data'
  62.  
  63.  
  64.  
  65. def dumps_bool(obj):
  66.     return 'b%d' % int(obj)
  67.  
  68.  
  69. def dumps_int(obj):
  70.     return 'i%s;' % obj
  71.  
  72.  
  73. def dumps_float(obj):
  74.     return 'f%r;' % obj
  75.  
  76.  
  77. def dumps_str(obj):
  78.     return 's%s:%s' % (len(obj), obj)
  79.  
  80.  
  81. def dumps_unicode(obj):
  82.     obj = obj.encode('utf-8')
  83.     return 'u%s:%s' % (len(obj), obj)
  84.  
  85.  
  86. def dumps_list(obj, _dt = None):
  87.     return [] % []([ dumps(val, _dt) for val in obj ])
  88.  
  89.  
  90. def dumps_tuple(obj, _dt = None):
  91.     return [] % []([ dumps(val, _dt) for val in obj ])
  92.  
  93.  
  94. def dumps_dict(obj, _dt = None):
  95.     res = []
  96.     keys = sorted(obj.keys())
  97.     append = res.append
  98.     for key in keys:
  99.         val = obj[key]
  100.         append(dumps(key, _dt))
  101.         append(dumps(val, _dt))
  102.     
  103.     return 'd%s;' % ''.join(res)
  104.  
  105.  
  106. def dumps_none(obj):
  107.     return 'n'
  108.  
  109.  
  110. def loads_bool(str, pos):
  111.     return (bool(int(str[pos + 1])), pos + 2)
  112.  
  113.  
  114. def loads_int(str, pos):
  115.     endpos = str.index(';', pos)
  116.     return (int(str[pos + 1:endpos]), endpos + 1)
  117.  
  118.  
  119. def loads_float(str, pos):
  120.     endpos = str.index(';', pos)
  121.     return (float(str[pos + 1:endpos]), endpos + 1)
  122.  
  123.  
  124. def loads_str(str, pos):
  125.     startpos = str.index(':', pos) + 1
  126.     endpos = startpos + int(str[pos + 1:startpos - 1])
  127.     return (str[startpos:endpos], endpos)
  128.  
  129.  
  130. def loads_unicode(str, pos):
  131.     startpos = str.index(':', pos) + 1
  132.     endpos = startpos + int(str[pos + 1:startpos - 1])
  133.     return (str[startpos:endpos].decode('utf-8'), endpos)
  134.  
  135.  
  136. def loads_list(str, pos, _lt = loads_table):
  137.     pos += 1
  138.     res = []
  139.     append = res.append
  140.     while str[pos] != ';':
  141.         (obj, pos) = _lt[str[pos]](str, pos)
  142.         append(obj)
  143.     return (res, pos + 1)
  144.  
  145.  
  146. def loads_tuple(str, pos, _lt = loads_table):
  147.     pos += 1
  148.     res = []
  149.     append = res.append
  150.     while str[pos] != ';':
  151.         (obj, pos) = _lt[str[pos]](str, pos)
  152.         append(obj)
  153.     return (tuple(res), pos + 1)
  154.  
  155.  
  156. def loads_dict(str, pos, _lt = loads_table):
  157.     pos += 1
  158.     res = { }
  159.     while str[pos] != ';':
  160.         (key, pos) = _lt[str[pos]](str, pos)
  161.         (val, pos) = _lt[str[pos]](str, pos)
  162.         res[key] = val
  163.     return (res, pos + 1)
  164.  
  165.  
  166. def loads_none(str, pos):
  167.     return (None, pos + 1)
  168.  
  169. dumps_table.update({
  170.     bool: dumps_bool,
  171.     int: dumps_int,
  172.     long: dumps_int,
  173.     float: dumps_float,
  174.     str: dumps_str,
  175.     unicode: dumps_unicode,
  176.     list: dumps_list,
  177.     tuple: dumps_tuple,
  178.     dict: dumps_dict,
  179.     type(None): dumps_none })
  180. loads_table.update({
  181.     'b': loads_bool,
  182.     'i': loads_int,
  183.     'f': loads_float,
  184.     's': loads_str,
  185.     'u': loads_unicode,
  186.     'l': loads_list,
  187.     't': loads_tuple,
  188.     'd': loads_dict,
  189.     'n': loads_none })
  190.